<?php

require("../function/util.php");

$om_id 					= $_POST['om_id'];
$om_image_old 			= $_POST['om_image_old'];

$om_weight 				= addslashes($_POST['om_weight']);
$om_name 				= addslashes($_POST['om_name']);
$om_price 				= addslashes($_POST['om_price']);
$om_type 				= $_POST['om_type'];
$om_status 				= $_POST['om_status'];
$current_time 			= time();

$om_image_type 			= explode('.' , $_FILES["om_image"]["name"]);

$om_image_filename 		= "om_image_" . $current_time . "." . $om_image_type[1];

$err_msg = "";

if (strlen(trim($om_weight)) == 0) {
    $err_msg .= "Please fill in Weight<br/>";
}

if (strlen(trim($om_name)) == 0) {
    $err_msg .= "Please fill in Name<br/>";
}

if (strlen(trim($om_price)) == 0) {
    $err_msg .= "Please fill in Price<br/>";
}

if ($_FILES["om_image"]["size"] > 0)
{
    if ( !( $_FILES["om_image"]["type"] == "image/jpeg" || $_FILES["om_image"]["type"] == "image/pjpeg" || $_FILES["om_image"]["type"] == "image/png" || $_FILES["om_image"]["type"] == "image/gif" ) ) 
	{
        $err_msg .= "Image file must be jpeg, png or gif<br/>";
    }

    if ( !( $_FILES["om_image"]["size"] < 2000000 ) )
	{
        $err_msg .= "Image file is too large<br/>";
    }

    if ( $_FILES["om_image"]["error"] > 0 )
	{
        $err_msg .= "Image File Error!<br/>";
    }
} else
{
       $om_image_filename = $om_image_old;
}

if (strlen($err_msg) == 0) 
{
	$sql = "UPDATE onlinemarket
			SET om_type 			='$om_type',
				om_weight 			='$om_weight',
				om_image 			='$om_image_filename',
				om_name 			='$om_name',
				om_price 			='$om_price',
				om_status 			='$om_status'
			WHERE om_id = '$om_id'";
	if(runSQL($sql))
	{

		if ( $_FILES["om_image"]["size"] > 0 )
		{
			$thumbfile = FILE_LOCATION . THUMB_LOCATION . "onlinemarket/" . "om_image_" . $current_time . "." . $om_image_type[1];
			if (move_uploaded_file($_FILES["om_image"]["tmp_name"], $thumbfile)) 
			{
				$small_thumbnail_image = 1;
			}
		}
?>
		<script type="text/javascript">
			alert("Edit Product Successful!");
				top.document.getElementById('cancel_button').click();
		</script>
<?php
	} else
	{
?>
		<script type="text/javascript">
			alert("Edit Product can't not be performed right now!");
		</script>
<?php
	}
} else
{
?>
    <script type="text/javascript">
        top.document.getElementById('error').innerHTML = "<?= $err_msg ?>";
    </script>
<?php
}
?>